package com.zl.smartmall.library;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.listener.GetNoticesListener;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;

import android.content.Context;

/**
 * 公告管理相关操作接口
 * @author 陈希然
 *
 */
public class INotice {

	private static final String TAG = IAddress.class.getSimpleName();
	private static INotice mInstance;
	private static Lock mLock;
	private Context mContext;
	
	static{
		mLock = new ReentrantLock();
	}
	
	private INotice(Context context) {
		mContext = context;
	}
	
	public static INotice getInstance() {
		return mInstance;
	}
	
	public static void init(Context context) {
		if(mInstance == null){
    		mLock.lock();
    		try{
				if(mInstance == null){
					mInstance = new INotice(context);
				}
    		}finally{
    			mLock.unlock();
    		}
    	}
	}
	
	/**
	 * 获取公告列表
	 * @param communityId 小区id
	 * @param page 页面; 起始值为1
	 * @param getNoticesListener 回调监听器
	 */
	public void getNotices(int communityId, int page, GetNoticesListener getNoticesListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			getNoticesListener.onNoNetWork();
			
		}else {
			RequestParams params = new RequestParams();
			params.put("page", page);
			params.put("cmid", communityId);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_NOTICES, params, getNoticesListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_GET_NOTICES + "?" + params);
		}
	}
}
